package jmetal.metaheuristics.singleObjective.panmitic;

import java.util.Comparator;
import java.util.concurrent.ForkJoinPool;

import jmetal.core.Algorithm;
import jmetal.core.Operator;
import jmetal.core.Problem;
import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.util.JMException;
import jmetal.util.comparators.ObjectiveComparator;

public class PanmiticFTGA extends Algorithm {

	public static int evaluationsTotal;

	/**
	 * 
	 * Constructor Create a new GGA instance.
	 * 
	 * @param problem
	 *            Problem to solve.
	 */
	public PanmiticFTGA(Problem problem) {
		super(problem);
		evaluationsTotal = 0;
	} // GGA

	/**
	 * Execute the GGA algorithm
	 * 
	 * @throws JMException
	 */
	public synchronized SolutionSet execute() throws JMException, ClassNotFoundException {
		int populationSize;
		int maxEvaluations;
		int evaluations;

		SolutionSet population;
		SolutionSet offspringPopulation;

		Operator mutationOperator;
		Operator crossoverOperator;
		Operator selectionOperator;

		Comparator comparator;
		comparator = new ObjectiveComparator(0); // Single objective comparator

		// Read the params
		populationSize = ((Integer) this.getInputParameter("populationSize")).intValue();
		maxEvaluations = ((Integer) this.getInputParameter("maxEvaluations")).intValue();

		// Initialize the variables
		population = new SolutionSet(populationSize);
		offspringPopulation = new SolutionSet(populationSize);

		evaluations = 0;

		// Read the operators
		mutationOperator = this.operators_.get("mutation");
		crossoverOperator = this.operators_.get("crossover");
		selectionOperator = this.operators_.get("selection");

		// Create the initial population
		Solution newIndividual;
		for (int i = 0; i < populationSize; i++) {
			newIndividual = new Solution(problem_);
			problem_.evaluate(newIndividual);
			evaluations++;
			evaluationsTotal++;
			population.add(newIndividual);
		} // for

		// Sort population
		population.sort(comparator);
		// Criando o Pool
		ForkJoinPool pool = new ForkJoinPool();
		// for (int i = 0; i < 3; i++) {
		while (evaluations < maxEvaluations) {
			// if ((evaluations % 10) == 0) {
			// System.out.println(evaluations + ": " +
			// population.get(0).getObjective(0));
			// } //

			// Criando uma instancia
			PanmiticFTGAParallelUnit unit = new PanmiticFTGAParallelUnit(mutationOperator, crossoverOperator,
					selectionOperator, population, this, populationSize);

			offspringPopulation = pool.invoke(unit);

			// Copy the best two individuals to the offspring population
			offspringPopulation.add(new Solution(population.get(0)));
			offspringPopulation.add(new Solution(population.get(1)));

			// The offspring population becomes the new current population
			population.clear();
			for (int i = 0; i < populationSize; i++) {
				population.add(offspringPopulation.get(i));
			}

			offspringPopulation.clear();
			population.sort(comparator);
			evaluations += (populationSize - 2);
			evaluationsTotal += (populationSize - 2);

		} // while

		// Return a population with the best individual
		SolutionSet resultPopulation = new SolutionSet(1);
		resultPopulation.add(population.get(0));
		return resultPopulation;
	}
}
